package com.whu.tcx01.utils;

import com.whu.tcx01.pojo.MyNode;
import com.whu.tcx01.road.RoadPojo;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.List;
import java.util.Map;

@Slf4j
public class SaveNodes {

    //导出观测概率
    public static void exportTxt(List<MyNode> nodeList, String filePath, String fileName) {
        FileOutputStream fos = null;
        OutputStreamWriter dos = null;

        try {
            File file = new File(filePath +fileName + ".txt");
            if (!file.exists()) {
                file.createNewFile();
            }

            fos = new FileOutputStream(file);
            dos = new OutputStreamWriter(fos);

            for (MyNode myNode : nodeList) {
                dos.write(myNode.getNodeid() + "\t");
                dos.write(myNode.getX() + "\t");
                dos.write(myNode.getY() + "\t");
                dos.write("\n");
            }
            log.info("保存完成！");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fos.flush();
                dos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    //导出观测概率
    public static void exportTxt2(Map<Long, List<RoadPojo>> map, String filePath, String fileName) {
        FileOutputStream fos = null;
        OutputStreamWriter dos = null;

        try {
            File file = new File(filePath +fileName + ".txt");
            if (!file.exists()) {
                file.createNewFile();
            }

            fos = new FileOutputStream(file);
            dos = new OutputStreamWriter(fos);
            for (Long mapKey : map.keySet()) {
                List<RoadPojo> list = map.get(mapKey);
                for (RoadPojo roadPojo : list) {
                    dos.write(roadPojo.getRoadid()+"\t");
                    dos.write(roadPojo.getStart()+"\t");
                    dos.write(roadPojo.getStop()+"\t");
                    dos.write(roadPojo.getDirection()+"\t");
                    dos.write(roadPojo.getDistance()+"\t");
                    dos.write(roadPojo.getPathclass()+"\t");
                    dos.write(roadPojo.getSpeed()+"\n");
                }
            }
            log.info("保存完成！");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fos.flush();
                dos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void exportTxt3(List<RoadPojo> list, String filePath, String fileName) {
        FileOutputStream fos = null;
        OutputStreamWriter dos = null;

        try {
            File file = new File(filePath +fileName + ".txt");
            if (!file.exists()) {
                file.createNewFile();
            }

            fos = new FileOutputStream(file);
            dos = new OutputStreamWriter(fos);

            for (RoadPojo roadPojo : list) {
                dos.write(roadPojo.getRoadid()+"\t");
                dos.write(roadPojo.getStart()+"\t");
                dos.write(roadPojo.getStop()+"\t");
                dos.write(roadPojo.getDirection()+"\t");
                dos.write(roadPojo.getDistance()+"\t");
                dos.write(roadPojo.getPathclass()+"\t");
                dos.write(roadPojo.getSpeed()+"\n");
            }
            log.info("保存完成！");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fos.flush();
                dos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
